package com.aisile.crowd_funding01.system.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.aisile.crowd_funding01.system.shiro.MyRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @标题 ShiroConfig
 * @作者 Mr.chen
 * @创建日期 2018/11/15 9:08
 * @版本号 1.0
 **/
@Configuration
public class ShiroConfig {
    /**
     * 配置shiro 过滤器
     * @return
     */
    @Bean
    ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);//过滤器 实现核心管理器
        shiroFilterFactoryBean.setLoginUrl("/login");//登录页面路径
        shiroFilterFactoryBean.setSuccessUrl("/index");//系统登录成功后的首页
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");//没有权限之后的跳转路径
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/login","anon");    //不拦截
        filterChainDefinitionMap.put("/css/**", "anon");  //不拦截
        filterChainDefinitionMap.put("/js/**", "anon");   //不拦截
        filterChainDefinitionMap.put("/fonts/**", "anon");//不拦截
        filterChainDefinitionMap.put("/img/**", "anon");  //不拦截
        filterChainDefinitionMap.put("/druid/**", "anon");//不拦截
        filterChainDefinitionMap.put("/upload/**", "anon");//不拦截
        filterChainDefinitionMap.put("/files/**", "anon");//不拦截
        filterChainDefinitionMap.put("/logout", "logout");//不拦截
        filterChainDefinitionMap.put("/loginSign", "anon");//不拦截
        filterChainDefinitionMap.put("/", "anon");//不拦截
        filterChainDefinitionMap.put("/**", "authc");//除上面路径外  全拦截
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    /**
     * ShiroDialect，为了在thymeleaf里使用shiro的标签的bean
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
    /**
     * 配置核心管理控制器
     * @return
     */
    @Bean
    SecurityManager securityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm());
        return defaultWebSecurityManager;
    }

    /**
     * 定义自定义的域  域中写了 认证的方法   授权的方法
     * @return
     */
    @Bean
    MyRealm myRealm(){
        MyRealm myRealm = new MyRealm();
        return myRealm;
    }

    /**
     * 管理shiro一些bean的生命周期。
     * @return
     */
    @Bean
    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }
    /**
     * 开启shiro aop注解支持.
     * 使用代理方式;所以需要开启代码支持;
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
